
#include "CInput.h"
#include <QMessageBox>

CInput::CInput(QWidget *parent)
{
    this->parent = parent;
    windowDeactivatedSignal = false;
}

CInput::~CInput()
{
    x = y = 0.0;
    sx = sy = 1.0;
}

void CInput::KeyPressedEvent(QKeyEvent *e)
{
    if ( keyDownTable.contains(e->key()) )
        keyDownTable[e->key()] = true;
    else
        keyDownTable.insert(e->key(),true);
    keyDownQueue.enqueue( e->key() );
}

void CInput::KeyReleaseEvent(QKeyEvent *e)
{
    if ( keyDownTable.contains(e->key()) )
        keyDownTable[e->key()] = false;
    else
        keyDownTable.insert(e->key(),false);
    keyUpQueue.enqueue( e->key() );
}

void CInput::mousePressEvent(QMouseEvent* e)
{
    if ( buttonDownTable.contains(e->button()) )
        buttonDownTable[e->button()] = true;
    else
        buttonDownTable.insert( e->button(), true );
    buttonDownQueue.enqueue( e->button() );
}

void CInput::mouseReleaseEvent(QMouseEvent *e)
{
    if ( buttonDownTable.contains(e->button()) )
        buttonDownTable[e->button()] = false;
    else
        buttonDownTable.insert( e->button(), false );
    buttonUpQueue.enqueue( e->button() );
}

void CInput::windowDeactivateEvent()
{
    windowDeactivatedSignal = true;
    keyDownTable.clear();
    buttonDownTable.clear();
}

void CInput::handleInput()
{
    static QPoint d;
    static QPoint pos;

    d = QCursor::pos() - pos;
    pos = QCursor::pos();    
    mdx = d.x(); mdy = d.y();
    QPoint p = parent->mapFromGlobal(pos);
    mx = p.x(); my = p.y();

    if ( mdx == 0 && mdy == 0 )
        mouseMoved = false;
    else
        mouseMoved = true;

    keyDownList.clear();
    while( !keyDownQueue.empty() )
        keyDownList.append( keyDownQueue.dequeue() );
    keyUpList.clear();
    while( !keyUpQueue.empty() )
        keyUpList.append( keyUpQueue.dequeue() );
    buttonDownList.clear();
    while( !buttonDownQueue.empty() )
        buttonDownList.append( buttonDownQueue.dequeue() );
    buttonUpList.clear();
    while( !buttonUpQueue.empty() )
        buttonUpList.append( buttonUpQueue.dequeue() );

    windowDeactivated = windowDeactivatedSignal;
    windowDeactivatedSignal = false;
}

bool CInput::isKeyPressed( int key ) const
{
    if ( !keyDownTable.contains(key) )
        return false;
    return keyDownTable[key];
}

bool CInput::wasKeyPressed( int key ) const
{
    return keyDownList.contains(key);
}

bool CInput::wasKeyReleased( int key ) const
{
    return keyUpList.contains(key);
}

bool CInput::isButtonPressed( int key ) const
{
    if ( !buttonDownTable.contains(key) )
        return false;
    return buttonDownTable[key];
}

bool CInput::wasButtonPressed( int key ) const
{
    return buttonDownList.contains(key);
}

bool CInput::wasButtonReleased( int key ) const
{
    return buttonUpList.contains(key);
}

bool CInput::hasScreenLostFocus() const
{
    return windowDeactivated;
}

QPoint CInput::getCursorPos() const
{
    return QPoint( mx*sx + x, my*sy + y );
}

QPoint CInput::getCursorChange() const
{
    return QPoint( mdx*sx, mdy*sy );
}

bool CInput::hasMouseMoved() const
{
    return mouseMoved;
}

void CInput::setScaPos( double x, double  y, double sx, double sy ) {
    this->x = x;
    this->y = y;
    this->sx = sx;
    this->sy = sy;
}

